const Article = require('../model/Article')
const User = require('../model/User')
const Image = require('../model/Image')

async function getBlogList(page = 1, size = 5) {
    const list = await Article.findAll({
        order: [['id', 'DESC']],
        offset: (page-1)*size,
        limit: parseInt(size)
    })
    let blogList = {
        statusCode: 0,
        statusMsg: []
    }
    for(let item of list) {
        let itemTem = item.dataValues
        let tem = {
            list: Object,
            user: Object
        }
        let user = (await User.findOne({
            where: {
                id: itemTem.userId
            }
        })).dataValues
        if(!user) {
            continue
        }
        let image = (await Image.findOne({
            where: {
                id: user.portraitId
            }
        })).dataValues
        user.image = image
        tem.list = itemTem
        tem.user = user
        delete tem.user.password
        blogList.statusMsg.push(tem) 
    }
    blogList.statusCode = 1
    return blogList
}

module.exports = getBlogList